package k4_thread.demo03;

public class SaleTicketByRunnable1 implements Runnable {

    //初始化一共有100张票,因为车票是所有火车站共享的，必须使用static进行修饰
    private int totalTickets = 100;


    public synchronized void saleTicket(){
        System.out.println(Thread.currentThread().getName()+"卖了一张票，剩余"+ --totalTickets+"张票");
    }

    @Override
    public void run() {

        while(true){
            try {
                if(totalTickets<=0){
                    break;
                }
                saleTicket();
                Thread.sleep(200);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

class Test2{
    public static void main(String[] args) {

        Runnable saleTicket = new SaleTicketByRunnable1();

        new Thread(saleTicket,"南京").start();
        new Thread(saleTicket,"广州").start();
        new Thread(saleTicket,"南宁").start();

    }
}
